/**
 * EditTakenCompanyInfoForm class.
 */
package pits.form;

import java.util.ArrayList;
import java.util.List;

import javax.portlet.ActionRequest;

import pits.dto.DepartexpDTO;
import pits.dto.EmployeeDTO;
import pits.util.Errand;

/**
 * @author ltd
 * 
 */
public class EditTakenCompanyInfoForm {

	private int departID;
	private int preDepartID;
	private String txtRole;
	private String txtStartDate;
	private String txtEndDate;
	private String txtDescription;

	public EditTakenCompanyInfoForm() {

	}

	public EditTakenCompanyInfoForm(ActionRequest request) {
		pits.util.Errand errand = new Errand();
		if (request.getParameter("txtDepartID") != null) {
			setDepartID(Integer.parseInt(request.getParameter("txtDepartID")));
		}
		if (request.getParameter("preDepartID") != null) {
			setPreDepartID(Integer.parseInt(request.getParameter("preDepartID")));
		}
		if (request.getParameter("txtStartDate") != null) {
			setStartDate(errand.parseDate(request.getParameter("txtStartDate")));
		}
		if (request.getParameter("txtEndDate") != null) {
			setEndDate(errand.parseDate(request.getParameter("txtEndDate")));
		}
		if (request.getParameter("txtRole") != null) {
			setRole(request.getParameter("txtRole"));
		}
		if (request.getParameter("txtDescription") != null) {
			setDescription(request.getParameter("txtDescription"));
		}
	}

	public void setDepartID(int departID) {
		this.departID = departID;
	}

	public int getDepartID() {
		return departID;
	}

	public int getPreDepartID() {
		return preDepartID;
	}

	public void setPreDepartID(int preDepartID) {
		this.preDepartID = preDepartID;
	}

	public void setRole(String txtRole) {
		this.txtRole = txtRole;
	}

	public String getRole() {
		return txtRole;
	}

	public void setStartDate(String txtStartDate) {
		this.txtStartDate = txtStartDate;
	}

	public String getStartDate() {
		return txtStartDate;
	}

	public void setEndDate(String txtEndDate) {
		this.txtEndDate = txtEndDate;
	}

	public String getEndDate() {
		return txtEndDate;
	}

	public void setDescription(String txtDescription) {
		this.txtDescription = txtDescription;
	}

	public String getDescription() {
		return txtDescription;
	}

	public List<String> validate(EmployeeDTO dtoEmp) {
		List<String> errMess = new ArrayList<String>();
		List<DepartexpDTO> depexps = dtoEmp.getDepartexps();
		int duplicate = 0;
		for (int i = 0; i < depexps.size(); i++) {
			if (this.departID == depexps.get(i).getDepartment().getDepartID()) {
				duplicate++;
				if ((this.departID == this.preDepartID && duplicate > 1)
				        || (this.departID != this.preDepartID && duplicate > 0)) {
					// new pits.util.Logger().write("depart " + this.departID +
					// " pre " + this.preDepartID + " dup " + duplicate);
					errMess.add("This experience is already exists! Update failed!");
					break;
				}
			}
		}
		// new pits.util.Logger().write("duplicate " + duplicate);
		return errMess;
	}
}
